package com.example.demo.controller;

import com.example.demo.entity.cart;
import com.example.demo.mapper.cartMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.*;

@CrossOrigin
@RestController
public class cartController {
    @Autowired
    cartMapper cartMapper;

    @GetMapping(path = "/selectcartbyuser/{user}")
    public List<cart> selectcartbyuser(@PathVariable("user") String user) {
        return cartMapper.selectcartByuser(user);
    }

    @GetMapping(path = "/addcart/{name}/{memory}/{colour}/{number}/{price}/{user}/{address}")
    public cart addcart(@PathVariable("name") String name, @PathVariable("memory") String memory, @PathVariable("colour") String colour,
                        @PathVariable("number") int number, @PathVariable("price") String price, @PathVariable("user") String user,
                        @PathVariable("address") String address) {
        cart c = new cart();
        c.setName(name);
        c.setMemory(memory);
        c.setColour(colour);
        c.setNumber(number);
        c.setPrice(price);
        c.setUser(user);
        c.setTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        c.setAddress(address);
        cartMapper.addcart(c);
        return c;
    }

    @GetMapping(path = "/delete/{time}/{user}")
    public String delete(@PathVariable("time") String time, @PathVariable("user") String user) {
        List<cart> list = new ArrayList<>();
        cart c = new cart();
        c.setTime(time);
        c.setUser(user);
        list.add(c);
        cartMapper.delete(list);
        return "cg";
    }
}
